<template>
  <div class="wrap">
    <HeaderBar :has-logo="true" />
    <div class="back">
      <div class="circle" @click="back">
        <i class="el-icon-back" />
      </div>
      <div class="last-page-name">预算编制数据</div>
    </div>
    <base-page v-bind="baseInfo">
      <template #searchBar>
        <filter-form
          ref="filterForm"
          :elements="elements"
          :show-reset="true"
          @submit="submit"
        />
      </template>
      <template #content>
        <div class="controller">
          <el-radio-group v-model="type" size="small" @change="typeChange">
            <el-radio-button label="1">业务职能</el-radio-button>
            <el-radio-button label="2">管理职能</el-radio-button>
          </el-radio-group>
          <div class="btns">
            <Button :loading="complateLoading" @click="handleComplate">完成编制</Button>
            <Button :loading="downloading2" icon="export" type="info" @click="downloadDemo">导入模板</Button>
            <Button :loading="uploadLoading" icon="import" @click="upload">导入</Button>
            <input v-show="false" ref="uploadPut" type="file" @change="fileChange">
            <Button :loading="downloading" icon="export" @click="download">导出</Button>
          </div>
        </div>
        <div class="table-box">
          <TableNew
            v-if="tableShow"
            :loading="tableLoading"
            :data="tableData"
            :columns="getTableEl"
            :pagination="pagination"
            @paginationChange="paginationChange"
          >
            <template #num="scope">
              <span>{{ scope.row.num || scope.row.id }}</span>
            </template>
            <template #yard="scope">
              <div v-if="scope.row.yard === 1">三香</div>
              <div v-else-if="scope.row.yard === 2">浒关</div>
            </template>
            <template #action="scope">
              <el-tooltip class="item" effect="dark" content="编辑" placement="top" :open-delay="600">
                <i class="el-icon-edit-outline action" @click="edit(scope.row)" />
              </el-tooltip>
            </template>
          </TableNew>
        </div>
        <Drawer
          ref="drawer"
          :visible="visible"
          width="400px"
          title="编辑预算编制数据"
          :footer-btns="footerBtns"
          @cancel="cancel"
          @confirm="confirm"
        >
          <div class="drw-body">
            <div class="top">
              <el-descriptions size="mini" :column="1">
                <el-descriptions-item label="预算科目编码">{{ currentRow ? currentRow.subjectCode : '' }}</el-descriptions-item>
                <el-descriptions-item label="预算科目名称">{{ currentRow ? currentRow.subjectName : '' }}</el-descriptions-item>
                <el-descriptions-item label="部门编码">{{ currentRow ? currentRow.departmentCode : '' }}</el-descriptions-item>
                <el-descriptions-item label="部门名称">{{ currentRow ? currentRow.departmentName : '' }}</el-descriptions-item>
              </el-descriptions>
            </div>
            <el-divider class="divider" />
            <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="100px">
              <el-form-item label="编辑月份" prop="month">
                <el-date-picker
                  v-model="form.month"
                  type="month"
                  format="MM"
                  popper-class="monthStyle"
                  value-format="MM"
                  placeholder="请选择月"
                  @change="selectMonth"
                />
              </el-form-item>
              <template v-if="form.month">
                <el-card class="box-card" header="三香">
                  <el-form-item label="一下" prop="san_1">
                    <el-input-number v-model="form.san_1" style="width:100%;text-align: left;" :controls="false" @change="calculate" />
                  </el-form-item>
                  <el-form-item label="二下" prop="san_2">
                    <el-input-number v-model="form.san_2" style="width:100%;text-align: left;" :controls="false" @change="calculate" />
                  </el-form-item>
                  <el-form-item label="调整" prop="san_3">
                    <el-input-number v-model="form.san_3" style="width:100%;text-align: left;" :controls="false" @change="calculate" />
                  </el-form-item>
                  <el-form-item label="预算取值" prop="san_4">
                    <el-input-number v-model="form.san_4" style="width:100%;text-align: left;" :controls="false" disabled />
                  </el-form-item>
                </el-card>
                <el-card class="box-card" header="浒关">
                  <el-form-item label="一下" prop="xu_1">
                    <el-input-number v-model="form.xu_1" style="width:100%;text-align: left;" :controls="false" @change="calculate" />
                  </el-form-item>
                  <el-form-item label="二下" prop="xu_2">
                    <el-input-number v-model="form.xu_2" style="width:100%;text-align: left;" :controls="false" @change="calculate" />
                  </el-form-item>
                  <el-form-item label="调整" prop="xu_3">
                    <el-input-number v-model="form.xu_3" style="width:100%;text-align: left;" :controls="false" @change="calculate" />
                  </el-form-item>
                  <el-form-item label="预算取值" prop="xu_4">
                    <el-input-number v-model="form.xu_4" style="width:100%;text-align: left;" :controls="false" disabled />
                  </el-form-item>
                </el-card>
                <el-card class="box-card" header="合计">
                  <el-form-item label="一下" prop="all_1">
                    <el-input-number v-model="form.all_1" style="width:100%;text-align: left;" :controls="false" disabled />
                  </el-form-item>
                  <el-form-item label="二下" prop="all_2">
                    <el-input-number v-model="form.all_2" style="width:100%;text-align: left;" :controls="false" disabled />
                  </el-form-item>
                  <el-form-item label="调整" prop="all_3">
                    <el-input-number v-model="form.all_3" style="width:100%;text-align: left;" :controls="false" disabled />
                  </el-form-item>
                  <el-form-item label="预算取值" prop="all_4">
                    <el-input-number v-model="form.all_4" style="width:100%;text-align: left;" :controls="false" disabled />
                  </el-form-item>
                </el-card>
              </template>
            </el-form>
          </div>
        </Drawer>
      </template>
    </base-page>
    <FooterBar />
  </div>
</template>

<script>
import baseFilterTable from '@/mixins/baseFilterTable'
import HeaderBar from '@/layout/HeaderBar.vue'
import FooterBar from '@/layout/FooterBar.vue'
import Decimal from 'decimal.js'

import {
  listYsDepartment
} from '@/api/public.js'

import {
  listYsBusinessBudgetDataPage,
  updateYsBusinessBudgetData,
  getManageList,
  updateYsManageBudgetData,
  exportExcelYsBusinessBudgetData,
  exportExcelYsManageBudgetDatademo,
  exportExcelYsBusinessBudgetDataDemo,
  exportExcelYsManageBudgetDataDemo,
  importExcelYsBusinessBudgetData,
  importExcelYsManageBudgetData,
  finishOrganization
} from '@/api/collectionManage/budgetingData'

export default {
  name: 'Dictionary', // 数据字典
  components: {
    HeaderBar,
    FooterBar
  },
  mixins: [baseFilterTable],
  props: {},
  data() {
    return {
      type: '1',
      year: '',
      baseInfo: {
        searchBarShow: true
      },
      tableData: [],
      tableEl: [
        {
          title: '预算科目编码',
          dataIndex: 'subjectCode',
          fix: true,
          fixed: true
        },
        {
          title: '预算科目名称',
          dataIndex: 'subjectName',
          fix: true,
          fixed: true
        },
        {
          title: '部门编码',
          dataIndex: 'departmentCode',
          fix: true,
          fixed: true
        },
        {
          title: '部门名称',
          dataIndex: 'departmentName',
          fix: true,
          fixed: true
        },
        {
          title: '1月',
          dataIndex: '01',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_1_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_1_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_1_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_1_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_1_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_1_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_1_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_1_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_1_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_1_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_1_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_1_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '2月',
          dataIndex: '02',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_2_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_2_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_2_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_2_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_2_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_2_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_2_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_2_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_2_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_2_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_2_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_2_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '3月',
          dataIndex: '03',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_3_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_3_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_3_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_3_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_3_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_3_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_3_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_3_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_3_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_3_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_3_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_3_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '4月',
          dataIndex: '04',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_4_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_4_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_4_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_4_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_4_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_4_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_4_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_4_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_4_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_4_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_4_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_4_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '5月',
          dataIndex: '05',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_5_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_5_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_5_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_5_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_5_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_5_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_5_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_5_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_5_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_5_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_5_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_5_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '6月',
          dataIndex: '06',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_6_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_6_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_6_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_6_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_6_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_6_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_6_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_6_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_6_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_6_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_6_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_6_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '7月',
          dataIndex: '07',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_7_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_7_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_7_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_7_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_7_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_7_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_7_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_7_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_7_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_7_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_7_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_7_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '8月',
          dataIndex: '08',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_8_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_8_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_8_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_8_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_8_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_8_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_8_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_8_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_8_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_8_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_8_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_8_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '9月',
          dataIndex: '09',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_9_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_9_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_9_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_9_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_9_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_9_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_9_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_9_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_9_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_9_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_9_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_9_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '10月',
          dataIndex: '10',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_10_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_10_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_10_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_10_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_10_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_10_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_10_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_10_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_10_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_10_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_10_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_10_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '11月',
          dataIndex: '11',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_11_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_11_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_11_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_11_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_11_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_11_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_11_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_11_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_11_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_11_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_11_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_11_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '12月',
          dataIndex: '12',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_12_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_12_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_12_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_12_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_12_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_12_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_12_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_12_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_12_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_12_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_12_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_12_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '全年',
          dataIndex: '13',
          type: 'hasMerge',
          align: 'center',
          fix: true,
          mergeList: [
            {
              title: '三香',
              dataIndex: 'san',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'san_13_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'san_13_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'san_13_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'san_13_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '浒关',
              dataIndex: 'xu',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'xu_13_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'xu_13_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'xu_13_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'xu_13_4',
                  align: 'right',
                  format: true
                }
              ]
            },
            {
              title: '合并',
              dataIndex: 'all',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '一下',
                  dataIndex: 'all_13_1',
                  align: 'right',
                  format: true
                },
                {
                  title: '二下',
                  dataIndex: 'all_13_2',
                  align: 'right',
                  format: true
                },
                {
                  title: '调整',
                  dataIndex: 'all_13_3',
                  align: 'right',
                  format: true
                },
                {
                  title: '预算取值',
                  dataIndex: 'all_13_4',
                  align: 'right',
                  format: true
                }
              ]
            }
          ]
        },
        {
          title: '操作',
          dataIndex: 'action',
          type: 'otherTag',
          fix: true,
          align: 'center',
          fixed: 'right'
        }
      ],
      elements: [
        {
          label: '综合搜索',
          type: 'input',
          key: 'all',
          value: '',
          placeholder: '请输入预算科目名称/预算科目编码',
          width: 284
        },
        {
          label: '部门',
          type: 'select',
          options: [],
          key: 'departmentCode',
          value: ''
        },
        {
          label: '月份',
          type: 'date-picker',
          mode: 'months',
          key: 'months',
          value: [],
          format: 'MM',
          popperClass: 'monthStyle',
          expiredDate: {
            disabledDate() {
              return false
            }
          }
        }
      ],
      visible: false,
      footerBtns: [
        {
          text: '保存',
          trigger: 'confirm'
        },
        {
          text: '取消',
          trigger: 'cancel',
          type: 'info'
        }
      ],
      form: {
        month: '',
        san_1: '',
        san_2: '',
        san_3: '',
        san_4: '',
        xu_1: '',
        xu_2: '',
        xu_3: '',
        xu_4: '',
        all_1: '',
        all_2: '',
        all_3: '',
        all_4: ''
      },
      rules: {
        month: [
          { required: true, messsage: '请选择月份', trigger: 'change' }
        ]
      },
      months: [],
      currentRow: null,
      downloading: false,
      downloading2: false,
      complateLoading: false,
      uploadLoading: false,
      tableShow: true
    }
  },
  computed: {
    getTableEl() {
      return this.tableEl.filter(i => {
        if (i.fix || this.months.includes(i.dataIndex)) return i
      })
    }
  },
  created() {
    this.year = this.$route.query.year
    this._listYsDepartment()
  },
  mounted() {},
  methods: {
    typeChange() {
      this.$refs.filterForm.submitFormFilter()
      this._listYsDepartment()
    },
    async getData(val) {
      const types = {
        '1': listYsBusinessBudgetDataPage,
        '2': getManageList
      }
      const months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
      this.months = val?.months && val?.months.length ? val.months : months
      this.tableLoading = true
      const params = {
        pageSize: this.pagination.pageSize,
        pageNum: this.pagination.current,
        year: this.year,
        all: val?.all || '',
        departmentCode: val?.departmentCode || ''
      }
      try {
        const { data } = await types[this.type](params)
        if (data.code === 200) {
          this.tableData = data.data.list
          this.pagination.total = data.data.total
          this.tableShow = false
          this.$nextTick(() => {
            this.tableShow = true
          })
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.tableLoading = false
        window.parent.postMessage('hideMask', '*')
      }
    },
    async _listYsDepartment() {
      try {
        const { data } = await listYsDepartment({
          status: this.type === '1' ? 1 : null,
          manageStatus: this.type === '1' ? null : 1
        })
        if (data.code === 200 && data.data?.length) {
          this.elements.find(i => i.key === 'departmentCode').options = []
          this.elements.find(i => i.key === 'departmentCode').options.push(...data.data.map(j => ({ label: j.departmentName, value: j.departmentCode })))
        } else {
          // this.$message.error(data.message || '部门获取失败')
        }
      } catch (error) {
        console.log(error)
      }
    },
    back() {
      // window.parent.postMessage('close', '*')
      this.$router.back()
    },
    async edit(item) {
      this.currentRow = item
      this.visible = true
    },
    selectMonth(val) {
      if (val) {
        Object.keys(this.form).forEach(i => {
          if (i !== 'month') {
            setTimeout(() => {
              const m = parseInt(val)
              const strs = i.split('_')
              this.form[i] = this.currentRow[`${strs[0]}_${m}_${strs[1]}`]
            })
          }
        })
      }
    },
    cancel() {
      this.$refs.form.resetFields()
      this.currentRow = null
      this.visible = false
    },
    confirm() {
      const types = {
        '1': updateYsBusinessBudgetData,
        '2': updateYsManageBudgetData
      }
      this.$refs.form.validate(async v => {
        if (v) {
          this.$refs.drawer.loadOn('confirm')
          const params = {
            year: this.year,
            month: this.form.month,
            subjectId: this.currentRow.subjectId,
            departmentId: this.currentRow.departmentId,
            san1: this.form.san_1,
            san2: this.form.san_2,
            san3: this.form.san_3,
            san4: this.form.san_4,
            xu1: this.form.xu_1,
            xu2: this.form.xu_2,
            xu3: this.form.xu_3,
            xu4: this.form.xu_4,
            all1: this.form.all_1,
            all2: this.form.all_2,
            all3: this.form.all_3,
            all4: this.form.all_4
          }
          try {
            const { data } = await types[this.type](params)
            if (data.code === 200) {
              this.$message.success(data.message || '操作成功')
              this.cancel()
              this.$refs.filterForm.submitFormFilter()
            } else {
              this.$message.error(data.message || '操作失败')
            }
          } catch (error) {
            console.log(error)
          } finally {
            this.$refs.drawer.loadOff('confirm')
          }
        }
      })
    },
    calculate() {
      if (this.form.san_2) {
        this.form.san_4 = Decimal(this.form.san_2).add(Decimal(this.form.san_3)).toNumber()
      } else {
        this.form.san_4 = this.form.san_1
      }
      if (this.form.xu_2) {
        this.form.xu_4 = Decimal(this.form.xu_2).add(Decimal(this.form.xu_3)).toNumber()
      } else {
        this.form.xu_4 = this.form.xu_1
      }
      this.form.all_1 = Decimal(this.form.san_1).add(Decimal(this.form.xu_1)).toNumber()
      this.form.all_2 = Decimal(this.form.san_2).add(Decimal(this.form.xu_2)).toNumber()
      this.form.all_3 = Decimal(this.form.san_3).add(Decimal(this.form.xu_3)).toNumber()
      this.form.all_4 = Decimal(this.form.san_4).add(Decimal(this.form.xu_4)).toNumber()
    },
    async handleComplate() {
      this.complateLoading = true
      const params = {
        year: this.year
      }
      try {
        const { data } = await finishOrganization(params)
        if (data.code === 200) {
          this.$message.success(data.message || '操作成功')
          this.$refs.filterForm.submitFormFilter()
        } else {
          this.$message.error(data.message || '操作失败')
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.complateLoading = false
      }
    },
    async download() {
      const types = {
        '1': exportExcelYsBusinessBudgetData,
        '2': exportExcelYsManageBudgetDatademo
      }
      const params = {
        year: this.year
      }
      this.downloading = true
      try {
        const res = await types[this.type](params)
        if (res.data.type === 'application/json') {
          this.$message.error('下载失败')
        } else {
          const blob = new Blob([res.data])
          const fileName = `${this.type === '1' ? '业务职能预算编制数据' : '管理职能预算编制数据'}` + '.xlsx'
          const URL = window.URL || window.webkitURL || window.moxURL
          if ('download' in document.createElement('a')) {
          // 非IE下载
            const elink = document.createElement('a')
            elink.download = fileName // 命名下载名称
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob) // 表示一个指定的file对象或Blob对象
            document.body.appendChild(elink)
            elink.click() // 点击触发下载
            URL.revokeObjectURL(elink.href) // 释放URL 对象
            document.body.removeChild(elink)
          } else {
          // IE10+下载
            navigator.msSaveBlob(blob, fileName)
          }
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.downloading = false
      }
    },
    async downloadDemo() {
      const types = {
        '1': exportExcelYsBusinessBudgetDataDemo,
        '2': exportExcelYsManageBudgetDataDemo
      }
      const params = {
        year: this.year
      }
      this.downloading2 = true
      try {
        const res = await types[this.type](params)
        if (res.data.type === 'application/json') {
          this.$message.error('下载失败')
        } else {
          const blob = new Blob([res.data])
          const fileName = `${this.type === '1' ? '业务职能预算编制数据导入模板' : '管理职能预算编制数据导入模板'}` + '.xlsx'
          const URL = window.URL || window.webkitURL || window.moxURL
          if ('download' in document.createElement('a')) {
          // 非IE下载
            const elink = document.createElement('a')
            elink.download = fileName // 命名下载名称
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob) // 表示一个指定的file对象或Blob对象
            document.body.appendChild(elink)
            elink.click() // 点击触发下载
            URL.revokeObjectURL(elink.href) // 释放URL 对象
            document.body.removeChild(elink)
          } else {
          // IE10+下载
            navigator.msSaveBlob(blob, fileName)
          }
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.downloading2 = false
      }
    },
    upload() {
      this.$refs.uploadPut.click()
    },
    fileChange(e) {
      const types = {
        '1': importExcelYsBusinessBudgetData,
        '2': importExcelYsManageBudgetData
      }
      if (!e.target.files.length) return this.$message.warning('请选择要上传的文件')
      const formData = new FormData()
      formData.append('file', e.target.files[0])
      const params = {
        year: this.year
      }
      types[this.type](params, formData).then(res => {
        if (res.data.type && res.data.type === 'application/json') {
          const reader = new FileReader()
          reader.onloadend = () => {
            const jsonData = JSON.parse(reader.result)
            if (jsonData.code && jsonData.code === 500) {
              this.$message.error(jsonData.message || '导入失败')
            } else if (jsonData.code && jsonData.code === 200) {
              this.$message.success(jsonData.message || '导入成功')
              this.$refs.filterForm.submitFormFilter()
            }
            this.$refs.uploadPut.value = ''
            this.uploadLoading = false
          }
          reader.readAsText(res.data)
        } else {
          // this.$message.success('操作成功')
          this.$refs.filterForm.submitFormFilter()
          this.$refs.uploadPut.value = ''
          if (res.data && res.headers['content-type'] === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8') {
            this.$message.warning('部分数据导入失败，请在《问题数据》文件中查看失败原因')
            const blob = new Blob([res.data])
            const fileName = '问题数据' + '.xlsx'
            const URL = window.URL || window.webkitURL || window.moxURL
            if ('download' in document.createElement('a')) {
            // 非IE下载
              const elink = document.createElement('a')
              elink.download = fileName // 命名下载名称
              elink.style.display = 'none'
              elink.href = URL.createObjectURL(blob) // 表示一个指定的file对象或Blob对象
              document.body.appendChild(elink)
              elink.click() // 点击触发下载
              URL.revokeObjectURL(elink.href) // 释放URL 对象
              document.body.removeChild(elink)
            } else {
            // IE10+下载
              navigator.msSaveBlob(blob, fileName)
            }
          } else {
            this.$message.success(res.data?.message)
          }
          this.uploadLoading = false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.wrap{
  height: 100vh;
  width: 100vw;
  background-color: #f0f2f5;
}
:deep(.container-group) {
  height: calc(100% - 134px) !important;
}
.back{
  height: 44px;
  background-color: #fff;
  padding: 8px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  .last-page-name {
    color: #888;
    margin-left: 16px;
  }
  .circle{
    height: 100%;
    width: 28px;
    border-radius: 50%;
    background-color: #2165f9;
    color: #fff;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
  }
}
.table-box{
  height: calc(100% - 44px);
  display: flex;
  flex-direction: column;
}
.red{
  color: #F56C6C;
}
.controller{
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .btns{
    display: flex;
    align-items: center;
    :deep(.ck-button) {
      margin-right: 8px;
    }
    :deep(.ck-button:last-of-type) {
      margin-right: 0;
    }
  }
}
.drw-body{
  height: 100%;
  overflow-y: auto;
  padding: 8px;
  :deep(.el-input-number input) {
    text-align: left;
  }
}
.divider{
  margin: 8px 0 !important;
}
.box-card{
  margin: 4px 20px;
  box-shadow: none;
  :deep(.el-card__header) {
    padding: 8px;
  }
  :deep(.el-card__body) {
    padding: 8px;
  }
}
</style>
